<template>
  <div class="app-container">
    <el-card>
      <el-form
        :model="listQuery"
        :inline="true"
        label-width="68px"
        size="small"
      >
        <el-form-item label="菜单名称">
          <el-input
            v-model="listQuery.title"
            placeholder="请输入菜单名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="菜单状态">
          <el-select v-model="listQuery.hidden" placeholder="请选择" clearable>
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button icon="el-icon-refresh" @click="handleReset"
            >重置</el-button
          >
          <el-button type="primary" icon="el-icon-search" @click="getList()"
            >查询</el-button
          >
        </el-form-item>
      </el-form>

      <el-button
        class="btn-add"
        type="primary"
        icon="el-icon-plus"
        @click="handleAddMenu()"
        size="mini"
      >
        添加菜单
      </el-button>
      <div class="table-container">
        <el-table
          ref="menuTable"
          style="width: 100%"
          :data="list"
          row-key="id"
          v-loading="listLoading"
        >
          <!-- <el-table-column label="编号" width="100" align="center">
            <template slot-scope="scope">{{ scope.row.id }}</template>
          </el-table-column> -->
          <el-table-column label="菜单名称 / 按钮名称" align="left">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>
          <el-table-column label="组件名称" align="center">
            <template slot-scope="scope">
              <div v-if="scope.row.type == 'M' || scope.row.type == 'C'">
                {{ scope.row.name }}
              </div>
              <div v-else>-</div>
            </template>
          </el-table-column>
          <el-table-column label="按钮字符" align="center">
            <template slot-scope="scope">
              <div v-if="scope.row.type == 'F'">{{ scope.row.name }}</div>
              <div v-else>-</div>
            </template>
          </el-table-column>
          <el-table-column label="菜单级数" align="center">
            <template slot-scope="scope">{{
              scope.row.level | levelFilter
            }}</template>
          </el-table-column>

          <el-table-column label="前端图标" align="center">
            <template slot-scope="scope"
              ><svg-icon :icon-class="scope.row.icon"></svg-icon
            ></template>
          </el-table-column>
          <el-table-column label="是否显示" align="center">
            <template slot-scope="scope">
              <el-switch
                @change="handleHiddenChange(scope.$index, scope.row)"
                :active-value="0"
                :inactive-value="1"
                v-model="scope.row.hidden"
              >
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="排序" align="center">
            <template slot-scope="scope">{{ scope.row.sort }}</template>
          </el-table-column>
          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleUpdate(scope.$index, scope.row)"
                >编辑
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleDelete(scope.$index, scope.row)"
                >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <detail ref="menuDetailRef" @handleCloseGetList="getList"></detail>
  </div>
</template>

<script>
import { deleteMenu, updateHidden, getMenuList } from "@/api/menu";
import { handleTree } from "@/utils/auth";
import detail from "./add.vue";
export default {
  components: {
    detail
  },
  name: "menuList",
  data() {
    return {
      id: null,
      statusOptions: [
        {
          label: "显示",
          value: 0
        },
        {
          label: "隐藏",
          value: 1
        }
      ],
      list: [],
      total: null,
      listLoading: true,
      listQuery: {
        title: "",
        hidden: ""
      },
      parentId: 0
    };
  },
  created() {
    this.getList();
  },
  watch: {
    //监听路由
    $route(route) {
      this.getList();
    }
  },
  methods: {
    handleReset() {
      this.listQuery = {
        name: "",
        status: ""
      };
      this.getList();
    },
    handleAddMenu() {
      this.$refs.menuDetailRef.openlog({
        isEdit: false
      });
      // this.$router.push("/ums/addMenu");
    },
    async getList() {
      this.listLoading = true;

      const { data: res } = await getMenuList(this.listQuery);
      this.handleTreeNodes(handleTree(res));
    },

    handleTreeNodes(nodes) {
      nodes.forEach(node => {
        if (node.children && node.children.length > 0) {
          // 如果节点有子节点，则递归处理子节点
          this.handleTreeNodes(node.children);
        } else {
          // 如果节点没有子节点，则删除 children 属性
          node.children = [];
        }
      });
      this.listLoading = false;
      this.list = nodes;
    },

    //修改菜单是否显示
    handleHiddenChange(index, row) {
      updateHidden(row.id, { hidden: row.hidden }).then(response => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000
        });
      });
    },
    handleUpdate(index, row) {
      this.$refs.menuDetailRef.openlog({
        isEdit: true,
        id: row.id
      });
    },
    //   删除菜单
    handleDelete(index, row) {
      this.$confirm("是否要删除该菜单", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteMenu(row.id).then(response => {
          this.$message({
            message: "删除成功",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      });
    }
  },
  filters: {
    // 菜单级数

    levelFilter(value) {
      if (value === 0) {
        return "目录";
      } else if (value === 1) {
        return "菜单";
      } else if (value == 2) {
        return "按钮";
      }
    },
    disableNextLevel(value) {
      if (value === 0) {
        return false;
      } else {
        return true;
      }
    }
  }
};
</script>

<style scoped>
.app-container {
  height: calc(100vh - 50px);
  background-color: #f5f5f5;
  padding: 20px;
}
</style>
